package com.lzh.blog.admin.amqp;

import com.lzh.lzhframework.dao.UserMapper;
import com.lzh.lzhframework.domain.dto.feedback.UpdateFeedbackReadStatusDto;
import com.lzh.lzhframework.domain.entity.User;
import com.lzh.lzhframework.domain.entity.mongo.UserFeedbackEntity;
import com.lzh.lzhframework.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统管理员消费者
 *
 * @author luzhiheng
 * @date 2023/11/6 17:49
 */
@Slf4j
@Component
@RabbitListener(queues = "testQueue10")
public class SystemAdminConsumer {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private UserMapper userMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "user.feedback.queue", durable = "true"),
            exchange = @Exchange(value = "user.feedback", type = ExchangeTypes.TOPIC),
            key = "user.feedback"
    ))
    @Transactional
    public void receiveUserFeedbackMsg(String msg) {
        String[] split = msg.split("#");
        Long feedbackId = Long.valueOf(split[0]);
        Long userId = Long.valueOf(split[1]);
        log.debug("receive user feedback msg，feedbackId：{}", feedbackId);
        // 保存管理员-用户反馈-是否已读记录
        List<User> adminList = userMapper.queryAdminList();
        adminList.forEach(admin -> {
            UserFeedbackEntity userFeedback = new UserFeedbackEntity();
            userFeedback.setUserId(userId);
            userFeedback.setAdminId(admin.getId());
            userFeedback.setIsRead(false);
            userFeedback.setFeedbackId(feedbackId);
            userFeedback.setCreateTime(new Date());
            mongoTemplate.insert(userFeedback);
        });
    }

    /**
     * 更新反馈消息为已读状态
     *
     * @param dto
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "feedback.read.queue", durable = "true"),
            exchange = @Exchange(value = "feedback.read", type = ExchangeTypes.TOPIC),
            key = "feedback.update.read"
    ))
    public void received(UpdateFeedbackReadStatusDto dto) {
        List<Long> feedbackIdList = dto.getFeedbackId();
        if (feedbackIdList.size() == 0) {
            return;
        }
        Update update = new Update();
        update.set("is_read", true);

        Query query = new Query(Criteria.where("admin_id").is(dto.getAdminId())
                .andOperator(Criteria.where("feedback_id").in(feedbackIdList))
                .andOperator(Criteria.where("is_read").is(false)));
        mongoTemplate.updateMulti(query, update, UserFeedbackEntity.class);
    }
}
